<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogVisible = true">添加分类</el-button>
      </div>
      <el-table :data="cateList" border stripe>
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称">
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名">
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="space">
            <el-button size="mini" type="primary" @click="showEditDialog(space.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="dle(space.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加 -->
    <el-dialog title="添加文章分类" :visible.sync="dialogVisible" width="30%" @closed="onAddClosed">
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="修改文章分类" :visible.sync="ediVisible" width="30%">
      <!-- 添加的表单 -->
      <el-form :model="ediVisiFrom" :rules="addRules" ref="edRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="ediVisiFrom.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="ediVisiFrom.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="ediVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      dialogVisible: false,
      ediVisible: false,
      //重新声明一个空对象,来储存传过来的对象数据
      ediVisiFrom: {},
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //发送axios得到数据
    async dateCateList () {
      const { data: res } = await this.$http('/my/cate/list')
      if (res.code !== 0) return
      this.cateList = res.data
    },
    //重置表单
    onAddClosed () {
      this.$refs.addRef.resetFields()
    },
    //添加
    addCate () {
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)

        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)

        this.dialogVisible = false
        this.dateCateList()
      })
    },
    //删除文章分类
    dle (id) {
      this.$confirm('此操作将删除, 是否继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
        if (res.code !== 0) return
        this.$message.success(res.message)
        this.dateCateList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //修改文章分类
    showEditDialog (obj) {
      this.ediVisible = true
      this.ediVisiFrom = { ...obj }
    },
    editCate () {
      this.$refs.edRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('/my/cate/info', this.ediVisiFrom)

        if (res.code !== 0) return this.$message.error(res.message)

        this.$message.success(res.message)

        this.dateCateList()

        this.ediVisible = false
      })
    }
  },
  created () {
    this.dateCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
